#include <type.h>

#ifndef  _ACORAL_LOAD_APP_H_
#define _ACORAL_LOAD_APP_H_

typedef acoral_u16    Elf32_Half;
typedef acoral_u32    Elf32_Addr;
typedef acoral_u32    Elf32_Off;
typedef acoral_u32    Elf32_Word;
typedef acoral_32     Elf32_Sword;

typedef struct dynamic
{
	Elf32_Sword d_tag;
	union
	{
		Elf32_Sword d_val;
		Elf32_Addr  d_ptr;
	}d_un;
} Elf32_Dyn;

#define ELF32_R_SYM(x)   ((x) >> 8)
#define ELF32_R_TYPE(x)  ((x) & 0xff)

//==================

typedef struct elf32_rel
{
	Elf32_Addr  r_offset;
	Elf32_Word  r_info;
} Elf32_Rel;

//==================

typedef struct elf32_rela
{
	Elf32_Addr  r_offset;
	Elf32_Word  r_info;
	Elf32_Sword r_addend;
} Elf32_Rela;

//==================

typedef struct elf32_sym
{
	Elf32_Word   st_name;
	Elf32_Addr   st_value;
	Elf32_Word   st_size;
	Elf32_Word   st_info;
	Elf32_Word   st_other;
	Elf32_Half   st_shndx;
} Elf32_Sym;

//======================

#define EI_NIDENT  16
typedef struct elf32_hdr
{
	unsigned char e_ident[EI_NIDENT];
	Elf32_Half    e_type;
	Elf32_Half    e_machine;
	Elf32_Word    e_version;
	Elf32_Addr    e_entry;
	Elf32_Off     e_phoff;
	Elf32_Off     e_shoff;
	Elf32_Word    e_flags;
	Elf32_Half    e_ehsize;
	Elf32_Half    e_phentsize;
	Elf32_Half    e_phnum;
	Elf32_Half    e_shentsize;
	Elf32_Half    e_shnum;
	Elf32_Half    e_shstrndx;
} Elf32_Ehd;

//========================

#define PF_R  0x4
#define PF_W  0x2
#define PF_X  0x1

typedef struct elf32_phdr
{
	Elf32_Word   p_type;
	Elf32_Off    p_offset;
	Elf32_Addr   p_vaddr;
	Elf32_Addr   p_paddr;
	Elf32_Word   p_filesz;
	Elf32_Word   p_memsz;
	Elf32_Word   p_flags;
	Elf32_Word   p_align;
} Elf32_Phdr;

/* sh_type */
#define SHT_NULL	0
#define SHT_PROGBITS	1
#define SHT_SYMTAB	2
#define SHT_STRTAB	3
#define SHT_RELA	4
#define SHT_HASH	5
#define SHT_DYNAMIC	6
#define SHT_NOTE	7
#define SHT_NOBITS	8
#define SHT_REL		9
#define SHT_SHLIB	10
#define SHT_DYNSYM	11
#define SHT_NUM		12
#define SHT_LOPROC	0x70000000
#define SHT_HIPROC	0x7fffffff
#define SHT_LOUSER	0x80000000
#define SHT_HIUSER	0xffffffff

/* sh_flags */
#define SHF_WRITE	0x1
#define SHF_ALLOC	0x2
#define SHF_EXECINSTR	0x4
#define SHF_MASKPROC	0xf0000000

/* special section indexes */
#define SHN_UNDEF	0
#define SHN_LORESERVE	0xff00
#define SHN_LOPROC	0xff00
#define SHN_HIPROC	0xff1f
#define SHN_ABS		0xfff1
#define SHN_COMMON	0xfff2
#define SHN_HIRESERVE	0xffff
 
typedef struct {
  Elf32_Word	sh_name;
  Elf32_Word	sh_type;
  Elf32_Word	sh_flags;
  Elf32_Addr	sh_addr;
  Elf32_Off	sh_offset;
  Elf32_Word	sh_size;
  Elf32_Word	sh_link;
  Elf32_Word	sh_info;
  Elf32_Word	sh_addralign;
  Elf32_Word	sh_entsize;
} Elf32_Shdr;

#define	EI_MAG0		0		/* e_ident[] indexes */
#define	EI_MAG1		1
#define	EI_MAG2		2
#define	EI_MAG3		3
#define	EI_CLASS	4
#define	EI_DATA		5
#define	EI_VERSION	6
#define	EI_OSABI	7
#define	EI_PAD		8

#define	ELFMAG0		0x7f		/* EI_MAG */
#define	ELFMAG1		'E'
#define	ELFMAG2		'L'
#define	ELFMAG3		'F'
#define	ELFMAG		"\177ELF"
#define	SELFMAG		4

#define	ELFCLASSNONE	0		/* EI_CLASS */
#define	ELFCLASS32	1       
#define	ELFCLASS64	2
#define	ELFCLASSNUM	3

#define ELFDATANONE	0		/* e_ident[EI_DATA] */
#define ELFDATA2LSB	1       
#define ELFDATA2MSB	2

#define EV_NONE		0		/* e_version, EI_VERSION */
#define EV_CURRENT	1
#define EV_NUM		2

#define ELFOSABI_NONE	0
#define ELFOSABI_LINUX	3

#ifndef ELF_OSABI
#define ELF_OSABI ELFOSABI_NONE
#endif

/* Notes used in ET_CORE */
#define NT_PRSTATUS	1
#define NT_PRFPREG	2
#define NT_PRPSINFO	3
#define NT_TASKSTRUCT	4
#define NT_AUXV		6
#define NT_PRXFPREG     0x46e62b7f      /* copied from gdb5.1/include/elf/common.h */
#define NT_PPC_VMX	0x100		/* PowerPC Altivec/VMX registers */
#define NT_PPC_SPE	0x101		/* PowerPC SPE/EVR registers */
#define NT_386_TLS	0x200		/* i386 TLS slots (struct user_desc) */


/* Note header in a PT_NOTE section */
typedef struct elf32_note {
  Elf32_Word	n_namesz;	/* Name size */
  Elf32_Word	n_descsz;	/* Content size */
  Elf32_Word	n_type;		/* Content type */
} Elf32_Nhdr;



void acoral_hexdump(char* filename, acoral_char(*CurDirList)[11]);
void acoral_readelf(char* filename, acoral_char(*CurDirList)[11]);
void acoral_loadapp(char* filename, acoral_u32 stack_size, acoral_char(*CurDirList)[11]);

#endif
